import common from "@common/components/vxeTable/render/common";
import utils from "@common/utils/utils";
import XEUtils from "xe-utils";
import VXETable from "vxe-table";
import CategoryTree from "@jxc/views/components/CategoryTree.vue";
import tableRender from "@common/components/common/tableRender";
import clothing from "@jxc/utils/clothing";
import constants from "@jxc/api/constants";
import commonApi from "@common/api/common";
import store from "@jxc/store";

const defaultProps = {
    tableColumn: [
        {
            title: "物料分类",
            field: "category_name",
            width: 120,
            align: "center",
            formatter({ row }) {
                let categorys = [];
                if (utils.isNotEmpty(row.parent_category_name)) {
                    categorys.push(row.parent_category_name);
                }
                if (utils.isNotEmpty(row.category_name)) {
                    categorys.push(row.category_name);
                }
                return categorys.join('-');
            },
        },
        {
            title: "物料编号",
            field: "code",
            width: 120,
            align: "center",
            formatter({ row }) {
                if (utils.isNotEmpty(row.goods_code)) {
                    return row.goods_code;
                }
                return row.code;
            },
        },
        {
            title: "物料名称",
            field: "name",
            width: 120,
            align: "center",
            formatter({ row }) {
                if (utils.isNotEmpty(row.goods_name)) {
                    return row.goods_name;
                }
                return row.name;
            },
        },
        {
            title: "规格",
            field: "spec",
            width: 70,
            align: "center",
        },
        {
            title: "型号",
            field: "model",
            width: 70,
            align: "center",
        },
        {
            title: "基本单位",
            field: "base_unit",
            align: "center",
            width: 70,
        },
        {
            title: "基本零售价",
            field: "base_sale_price",
            align: "center",
            width: 90,
            formatter: ({row}) => {
                if (utils.isNotEmpty(row.base_sale_price)) {
                    return Number(row.base_sale_price).toFixed(4);
                }
                return '';
            },
        },
        {
            title: "基本采购价",
            field: "base_buy_price",
            align: "center",
            width: 90,
            formatter: ({row}) => {
                if (utils.isNotEmpty(row.base_buy_price)) {
                    return Number(row.base_buy_price).toFixed(4);
                }
                return '';
            },
        },
    ],
    remoteMethod(e) {
        const _this = this;
        let url = constants.API_QueryJxcGoodsList;
        if (clothing.isClothing(store.state.jxc.setting)) {
            url = constants.API_QueryJxcClothingGoodsList;
        }
        let queryData = {
            with_price: '1',
            search_value: e.searchValue || '',
            pageNo: e.pageNo,
            pageSize: e.pageSize,
        };
        if (e.row && e.visible) {
            if (e.row.category_full_code) {
                queryData.category_full_code = e.row.category_full_code;
            }
            if (e.row.last_trade_product === 1) {
                queryData.last_trade_product = e.row.last_trade_product;
            }
        }
        const fun = () => {};
        return commonApi.requestData(url, queryData, fun, fun, fun, true).then(result => {
            if (result.code !== 0) {
                return Promise.reject(result);
            }
            return result;
        }).catch(() => {
            return {
                data: [],
                page: {
                    totalRows: 0,
                    pages: 0,
                },
            };
        });
    },
    dLeft(h, ctx, params) {
        let { row } = params;
        return h(CategoryTree, {
            props: {
                detail: true,
                showShrink: false,
                width: '200px',
            },
            on: {
                event(e) {
                    let categoryFullCode = null;
                    if (e.type === 'nodeClick' && e.data) {
                        categoryFullCode = e.data.full_code;
                    }
                    ctx.parent && ctx.parent.$set(row, 'category_full_code', categoryFullCode);
                    ctx.parent && typeof ctx.parent.getData === 'function' && ctx.parent.getData(true);
                },
            },
        });
    },
    dButtons(h, ctx, params) {
        let { row } = params;
        if (utils.isEmpty(row.last_trade_product)) {
            ctx.parent && ctx.parent.$set(row, 'last_trade_product', 0);
        }
        return tableRender.renderElRadio(h, params, {
            style: {
                margin: '0 5px',
            },
            value: row.last_trade_product,
            items: [
                { value: 0, text: '全部物料' },
                { value: 1, text: '客户最近交易物料' },
            ],
            input(value) {
                ctx.parent && ctx.parent.$set(row, 'last_trade_product', value);
                ctx.parent && typeof ctx.parent.getData === 'function' && ctx.parent.getData(true);
            },
        });
    },
    tableStyle: {
        width: 'calc(100% - 200px)',
    },
};

// 创建一个下拉表格渲染
VXETable.renderer.add("vProductDownDialog", {
    autofocus: ".vxe-input--inner",
    renderEdit(h, renderOpts, params) {
        // eslint-disable-next-line no-unused-lets
        const { row, column } = params;
        const { attrs, props, filter, type } = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        let check = typeof filter === 'undefined' || (typeof filter === 'function' && filter(row, column));
        if (!check) {
            return [h('span', { class: 'vxe-cell--label' }, cellValue)];
        }
        let compProps = $.extend({}, defaultProps, props, { params, value: cellValue });
        if (utils.isNotEmpty(type)) {
            let tableColumn = (compProps.tableColumn || []).map(item => $.extend({}, item));
            if (type === 'sale') {
                tableColumn = tableColumn.filter(item => item.field !== 'base_buy_price');
            } else if (type === 'buy') {
                tableColumn = tableColumn.filter(item => item.field !== 'base_sale_price');
            } else if (type === 'none') {
                tableColumn = tableColumn.filter(item => item.field !== 'base_sale_price');
                tableColumn = tableColumn.filter(item => item.field !== 'base_buy_price');
            }
            compProps.tableColumn = tableColumn;
            compProps.modelTableColumn = tableColumn.map(item => {
                const obj = $.extend({}, item);
                delete obj.width;
                obj.minWidth = item.width;
                return obj;
            });
        }
        return [
            h('v-down-dialog', {
                attrs: attrs,
                props: compProps,
                on: common.getEditOns(renderOpts, params),
                nativeOn: common.getNativeOns(renderOpts, params)
            })
        ];
    }
});
